/*
Created By VietRise on 16/6/2014
*/

#include "ScreenStart.h"
#include "ScreenLevel.h"
#include "ScreenHelp.h"
#include "ScreenInfomation.h"

ScreenStart::~ScreenStart()
{
	CC_SAFE_RELEASE(_buttons);
	CC_SAFE_RELEASE(_animationStart);
}

ScreenStart::ScreenStart()
{ 
	_screenSize = CCDirector::sharedDirector()->getWinSize();
	_startPress = false;

	createScreen();
	createActions();	

	setTouchEnabled(true);	

	_btnStart->runAction(_animationStart);
	_btnHelp->moveInOut();
	_btnLike->moveInOut();
	_btnInfo->moveInOut();
}

CCScene* ScreenStart::scene()
{
	CCScene* scene = CCScene::create();
	ScreenStart* layer = new ScreenStart();
	scene->addChild(layer);
	layer->release();
	return scene;
}

void ScreenStart::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{ 		
	CCTouch* touch = (CCTouch*)pTouches->anyObject();
	if (touch)
	{		
		CCPoint tap = touch->getLocation();
		if (_btnStart->boundingBox().containsPoint(tap))
		{			
			_startPress = true;
			_btnStart->stopAction(_animationStart);
			_btnStart->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("btn_start_06.png"));
		}

		Button* sprite;
		for (int i = 0; i < _buttons->count(); i++)
		{
			sprite = (Button*)_buttons->objectAtIndex(i);
			if (sprite->boundingBox().containsPoint(tap))
			{
				if (sprite->getState() == STATEBUTTON::sNormal)
				{
					sprite->setTouch(touch);
					sprite->moveIn();
				}
			}						
		}
	}
}

void ScreenStart::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	CCTouch* touch = (CCTouch*)pTouches->anyObject();
	if (touch)
	{
		CCPoint tap = touch->getLocation();

		if (_startPress)
		{
			// Move out
			if (!_btnStart->boundingBox().containsPoint(tap))
			{
				_startPress = false;
				_btnStart->runAction(_animationStart);
			}			
		}

		Button* sprite;
		for (int i = 0; i < _buttons->count(); i++)
		{
			sprite = (Button*)_buttons->objectAtIndex(i);
			// Move out
			if (sprite->getTouch())
			{
				if (!sprite->boundingBox().containsPoint(tap) && sprite->getState() == STATEBUTTON::sPress)
				{
					sprite->moveOut(false);
				}
			}
			// Move in
			if (sprite->boundingBox().containsPoint(tap))
			{
				if (sprite->getState() == STATEBUTTON::sNormal)
				{
					sprite->setTouch(touch);
					sprite->moveIn();
				}
			}
			
		}
	}
}

void ScreenStart::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{	
	CCTouch* touch = (CCTouch*)pTouches->anyObject();
	if (touch)
	{
		CCPoint tap = touch->getLocation();
		if (_btnStart->boundingBox().containsPoint(tap))
		{			
			CCScene* newScene = CCTransitionMoveInR::create(0.2f, ScreenLevel::scene());
			CCDirector::sharedDirector()->replaceScene(newScene);
		}

		Button* sprite;
		for (int i = 0; i < _buttons->count(); i++)
		{
			sprite = (Button*)_buttons->objectAtIndex(i);
			if (sprite->boundingBox().containsPoint(tap))
			{
				if (sprite->getTouch() && touch == sprite->getTouch())
				{
					if (sprite->getState() == STATEBUTTON::sPress)
					{
						sprite->moveOut(true);
					}
				}
			}				
		}
	}
}

void ScreenStart::createScreen()
{
	CCSprite* bgSprite = CCSprite::create("bg_start.png");
	bgSprite->setPosition(ccp(_screenSize.width*0.5f, _screenSize.height*0.5f));
	this->addChild(bgSprite, kBackground, kSpriteBackground);

	_gameBatchNode = CCSpriteBatchNode::create("sheet_sprite.png", 20);
	this->addChild(_gameBatchNode, kMiddleground);

	CCSprite* sprite = CCSprite::createWithSpriteFrameName("label_hitmouse.png");	
	sprite->setPosition(ccp(_screenSize.width*0.5f, _screenSize.height*0.8f));
	sprite->setScale(0.7f);
	sprite->setRotation(-6);
	_gameBatchNode->addChild(sprite);

	sprite = CCSprite::createWithSpriteFrameName("item_bamboo.png");
	sprite->setPosition(ccp(275, 101));
	_gameBatchNode->addChild(sprite, kForeground);	

	_btnStart = CCSprite::createWithSpriteFrameName("btn_start_06.png");
	_btnStart->setPosition(ccp(700, 100));
	_gameBatchNode->addChild(_btnStart, kMiddleground, kSpriteBtnStart);


	_buttons = CCArray::createWithCapacity(4);
	_buttons->retain();

	_btnHelp = Button::create(this, TYPEBUTTON::btnHelp);
	_btnHelp->setPosition(ccp(132, 57));
	_buttons->addObject(_btnHelp);
	_gameBatchNode->addChild(_btnHelp, kMiddleground, kSpriteBtnHelp);

	
	_btnLike = Button::create(this, TYPEBUTTON::btnLike);
	_btnLike->setPosition(ccp(236, 57));
	_buttons->addObject(_btnLike);
	_gameBatchNode->addChild(_btnLike, kMiddleground, kSpriteBtnLike);

	_btnInfo = Button::create(this, TYPEBUTTON::btnInfo);
	_btnInfo->setPosition(ccp(338, 57));
	_buttons->addObject(_btnInfo);
	_gameBatchNode->addChild(_btnInfo, kMiddleground, kSpriteBtnInfo);

	bool music = CCUserDefault::sharedUserDefault()->getBoolForKey("music");	
	if (music)
	{
		_btnMusicNormal = Button::create(this, TYPEBUTTON::btnMusicNormal);
		_btnMusicNormal->setPosition(ccp(438, 57));
		_buttons->addObject(_btnMusicNormal);
		_gameBatchNode->addChild(_btnMusicNormal, kMiddleground);
		_btnMusicNormal->moveInOut();
	}
	else
	{
		_btnMusicPress = Button::create(this, TYPEBUTTON::btnMusicPress);
		_btnMusicPress->setPosition(ccp(438, 57));
		_buttons->addObject(_btnMusicPress);
		_gameBatchNode->addChild(_btnMusicPress, kMiddleground);
		_btnMusicPress->moveInOut();
	}

}

void ScreenStart::createActions()
{
	// Create animation button Start
	CCAnimation* animation;
	animation = CCAnimation::create();
	CCSpriteFrame* frame;

	for (int i = 1; i <= 5; i++)
	{
		char szName[100] = { 0 };
		sprintf(szName, "btn_start_0%i.png", i);
		frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);
		animation->addSpriteFrame(frame);
	}
	animation->setDelayPerUnit(TIME_ANIMATION_BTNSTART / 5);
	animation->setRestoreOriginalFrame(true);
	CCAnimate* animate;
	animate  = CCAnimate::create(animation);
	_animationStart = CCRepeatForever::create((CCActionInterval*)animate);
	_animationStart->retain(); 		
}

void ScreenStart::setVisiableBtnMusicNormal()
{
	if (_btnMusicPress)
	{
		_buttons->removeObject(_btnMusicPress, true);
		_gameBatchNode->removeChild(_btnMusicPress, true);
		_btnMusicPress = NULL;
		_btnMusicNormal = Button::create(this, TYPEBUTTON::btnMusicNormal);
		_btnMusicNormal->setPosition(ccp(438, 57));
		_buttons->addObject(_btnMusicNormal);
		_gameBatchNode->addChild(_btnMusicNormal, kMiddleground);
	}
}

void ScreenStart::setVisiableBtnMusicPress()
{
	if (_btnMusicNormal)
	{
		_buttons->removeObject(_btnMusicNormal, true);
		_gameBatchNode->removeChild(_btnMusicNormal, true);
		_btnMusicNormal = NULL;
		_btnMusicPress = Button::create(this, TYPEBUTTON::btnMusicPress);
		_btnMusicPress->setPosition(ccp(438, 57));
		_buttons->addObject(_btnMusicPress);
		_gameBatchNode->addChild(_btnMusicPress, kMiddleground);
	}
}